
/*******************************************************************************
Title:			Seasonality Analysis for Kenya GE
Written by:		Corey Vernot
Purpose:		

Organization	0. Globals and cleaning
					0.1 Clean seasonal food security control
				2. Analysis
					2.1 Plot food security vs mental health in pre-period
					2.2 Show pre-trends after residualizing on seasonal FS
					2.3 Randomization distribution of residualized pre-trends resampling FS months
					2.4 Control for seasonality in main regressions
*******************************************************************************/

*		0. Cleaning
*-------------------------------------------------------------------------------

*			0.1 Data Prep
qui do "${code}/Data Preparation/dataprep_kenya_ge"

*			0.2 Average food security by month, survey round in 14-15, 16-17
*-------------------------------------------------------------------------------
keep if round < 2
replace round = 1.1 if date > mdy(5, 1, 2017)
gen month_seas = mdy(month(date), 15, year(date))
collapse (mean) ctrl_seas = p9_foodindex, by(month_seas round)
format month_seas %tdmy

tempfile month_seas
save `month_seas'

*			0.3 Assign average monthly values to each day of the calendar year
*-------------------------------------------------------------------------------
foreach i in 0 1 {
	preserve
	
	keep if round == `i'
	sort month_seas
	gen month = month(month_seas)
	ren ctrl_seas ctrl_seas`i'
	
	tempfile month_seas`i'
	save `month_seas`i'', replace

	
	sum month_seas
	loc min `r(min)'
	loc max `r(max)'
	loc range = `max' - `min' + 300 // Replicate values for 150 days before and after 
									// for smooth lowess at the edges of the data.
	clear
	
	set obs `range'
	gen date = `min' -150 + _n
	gen month = month(date)
	merge m:1 month using `month_seas`i''
	format date %tdmy
	
	tempfile daily`i'
	save `daily`i''
	
	restore
}

use `daily0'
append using `daily1'
gen day = doy(date)


*			0.4 Fit lowess to daily seasonal food security observations in both years
*-------------------------------------------------------------------------------
gen d =date
replace d = date - 365*2 if round == 1
lowess ctrl_seas0 d, bw(.1) gen(_ctrl_seas0)
lowess ctrl_seas1 d, bw(.1) gen(_ctrl_seas1)


*-------------------------------------------------------------------------------
*		1. Plot Seasonal Food Security
*-------------------------------------------------------------------------------
gen plotsamp = d > mdy(7, 1, 2014) & d < mdy(7,2,2015)



gl grset graphregion(color(white)) ylabel(, angle(0)) plotregion(margin(sides))

if 1 == 1 { //skip plotting?
format d %tdm
tw (line _ctrl_seas0 d if round == 0 & plotsamp, sort) ///
	(line _ctrl_seas1 d if round == 1 & plotsamp, sort) , ///
	legend(order(1 "2014-2015" 2 "2016-2017"))  $grset  ///
	ytitle(Food Security Index) 
	graph export  "${output}\seasonality/kenya_seas1.png", replace
	
format date %tdmy
tw (line _ctrl_seas0 date if round == 0 & plotsamp, sort) ///
	(line _ctrl_seas1 date if round == 1 & plotsamp, sort) , ///
	legend(order(1 "2014-2015" 2 "2016-2017")) $grset ///
	ytitle(Food Security Index) 
		graph export  "${output}\seasonality/kenya_seas0.png", replace
}
	
*-------------------------------------------------------------------------------
*		2. Plot Seasonal Food Security
*-------------------------------------------------------------------------------


gen ctrl_seas = _ctrl_seas0
replace ctrl_seas = _ctrl_seas1 if mi(ctrl_seas)
gen year = year(date) + day/365 if day < 366

*		2.1 Get dates for original sample
*-------------------------------------------------------------------------------
preserve
qui do "${code}/Data Preparation/dataprep_kenya_ge"
keep if round < 2
replace round = 1.1 if date > mdy(5, 1, 2017)
foreach i in 0 1 {
	sum date if round == `i'
	loc min`i' = `r(min)'
	loc max`i' = `r(max)'
}
restore
gen originalSamp = (date >= `min0' & date <= `max0') | (date >= `min1' & date <= `max1')
tab originalSamp


*		2. Get dates for original sample
*-------------------------------------------------------------------------------
reg ctrl_seas i.day year if originalSamp == 1

preserve
clear
set obs 1500
gen date = mdy(9,12,2017) + _n
gen day = doy(date)
gen year = year(date) + day/365
tempfile moredates
save `moredates', replace
restore
append using `moredates'
predict _ctrl_seas

tw (line _ctrl_seas date if day != 366 & date < mdy(11,1,2017), sort lpattern(dash)) ///
	(line ctrl_seas date if originalSamp == 1 & round == 0, sort ) ///
	(line ctrl_seas date if originalSamp == 1 & round == 1, sort) ///
	, legend(order(1 "Seasonality with Linear Time Trend" 2 "2014-15 Data" 3 "2016-17 Data"))
graph export  "${output}\seasonality/kenya_seas_withtimetrend.png", replace

	keep if !mi(_ctrl_seas) & day != 366
	replace _ctrl_seas = _ctrl_seas1 if originalSamp == 1 & round == 1
	replace _ctrl_seas = _ctrl_seas0 if originalSamp == 1 & round == 0
	keep date _ctrl_seas round originalSamp
	duplicates drop date _ctrl_seas, force
	isid date
	replace date = date - 15
	save "${raw}/kenya_ge/kenya_ge_seas", replace


